<template>
  <div>
    <!-- 面包屑导航区域 -->
    <div class="el-header">
      <el-menu background-color="#eee" text-color="#303133" class="el-menu-demo" mode="horizontal">
        <el-menu-item index="/home" @click="$router.push('/home')">首页</el-menu-item>
        <el-menu-item>平台收入明细</el-menu-item>
      </el-menu>
    </div>
    <!-- 面包屑导航区域 -->
    <el-card>
      <!-- 表单-条件查询区域 -->
      <el-form ref="form" :model="form" label-width="100px" inline sizi="mini">
        <el-form-item label="店铺ID/名称" prop="user_id">
          <el-input v-model.trim="form.user_id"></el-input>
        </el-form-item>
        <el-form-item label="交易类型" prop="income_type">
          <el-select v-model="form.income_type" placeholder="请选择" @change="onValue">
            <el-option v-for="(item, i) in options" :key="i" :label="item" :value="item"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="order_number">
          <el-input v-model.trim="form.order_number"></el-input>
        </el-form-item>
        <el-form-item label="日期">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.date1"></el-date-picker>
          </el-col>
          <el-col class="text" :span="2">至</el-col>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.date2"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="4">店铺提现金额:{{ total - usercash }} ></el-col>
        <el-col :span="4">用户提现金额:{{ usercash }}</el-col>
        <el-col :span="4">总提现金额:{{ total }}</el-col>
        <el-col :span="4">总提现金额:</el-col>
        <el-col :span="4">总提现金额:</el-col>
      </el-row>
      <!-- 数据表格展示区域 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column prop="income_type" label="交易类型"> </el-table-column>
        <el-table-column prop="user_id" label="店铺ID/用户ID"> </el-table-column>
        <el-table-column prop="username" label="店铺名称/昵称"> </el-table-column>
        <el-table-column prop="order_number" label="提现单号"> </el-table-column>
        <el-table-column prop="transaction_amount" label="提现金额"> </el-table-column>
        <el-table-column prop="trading_hour" label="交易时间"> </el-table-column>
      </el-table>
      <!-- 表格区域 -->
    </el-card>
  </div>
</template>
<script>
import dayjs from 'dayjs'
export default {
  data() {
    return {
      tableData: [],
      options: ['店铺订单', '素材收益', '素材抽佣', '店铺权益', '风格模板', '店铺订单'],
      form: {
        username: '',
        user_id: '',
        income_type: '',
        order_number: null,
        date1: '',
        date2: ''
      },
      value: '',
      dialogVisible: false
    }
  },
  created() {
    this.getSpendingList()
  },
  methods: {
    async getSpendingList() {
      const { data: res } = await this.$http.get('/income_details')
      console.log(res)
      this.tableData = res
    },
    async submitForm() {
      var str = ''
      this.tableData = []
      for (var k in this.form) {
        str += this.form[k]
        if (this.form[k]) {
          const { data: res } = await this.$http.get(`/income_details?${k}_like=${this.form[k]}`)
          console.log(res)
          res.forEach(item => {
            if (!this.tableData.find(subitem => subitem.id === item.id)) {
              this.tableData.push(item)
            }
          })
        }
      }
      if (this.form.income_type) {
        const { data: res } = await this.$http.get(`/income_details?income_type_like=${this.form.income_type}`)
        this.tableData = res
      }

      if (this.form.date1 || this.form.date2) {
        var startDate = ''
        var endDate = ''
        if (this.form.date1 !== '') {
          startDate = dayjs(this.form.date1).format('YYYY-MM-DD')
        }
        if (this.form.date2 !== '') {
          endDate = dayjs(this.form.date2).format('YYYY-MM-DD')
        }
        console.log(startDate, endDate)

        var filter = startDate ? 'trading_hour_gte=' + startDate : ''
        if (filter === '') {
          filter += endDate ? 'trading_hour_lte=' + endDate : ''
        } else {
          filter += endDate ? '&trading_hour_lte=' + endDate : ''
        }

        const { data: res } = await this.$http.get(`/income_details?${filter}`)
        console.log(res)
        res.forEach(item => {
          if (!this.tableData.find(subitem => subitem.id === item.id)) {
            this.tableData.push(item)
          }
        })
      }
      // 判断所有查询条件是否空
      if (!str || str === '') {
        this.getSpendingList()
      }
    },
    onValue() {
      console.log(this.value)
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.form.date1 = ''
      this.form.date2 = ''
      this.getSpendingList()
    }
  },
  computed: {
    total() {
      var num = 0
      this.tableData.forEach(item => {
        num += item.transaction_amount
      })
      return num
    },
    usercash() {
      const arr1 = this.tableData.filter(item => item.income_type === '用户提现')
      var usernum = 0
      arr1.forEach(item => {
        usernum += item.transaction_amount
      })
      return usernum
    }
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-top: 15px;
  margin-left: 15px;
}
.el-header {
  padding: 0;
}
.el-card {
  margin: 25px;
  min-width: 1200px im !important;
  .el-form {
    .el-button {
      margin-left: 30px;
    }
    .text {
      padding-left: 15px;
    }
  }
}
</style>
